'''
Created on Apr 4, 2011

@author: Akshay
'''
import os
import re
import time
from threading import Thread
import urllib as url

class get(Thread):
    def __init__(self, n):
        Thread.__init__(self)
        self.num = n
    def run(self):
        #print 'Running', self.num
        try:
            match = re.search(r'img src="(http://imgs.xkcd.com/comics/\S+\.(\w+))" title=\S+.+>',url.urlopen('http://www.xkcd.com/'+str(self.num)+'/').read())
        except IOError:
            #print str(self.num)+': Could Not Connect'
            self.status = 'Failed to establish connection'
            return
        except:
            self.status = 'Unknown Exception'
            return            
        if match:
            (image, extension) = (match.group(1), match.group(2))
            if os.path.exists(os.path.normpath('H:/xkcd2/timeout/'+str(self.num)+'.'+extension)):
                self.status = 'Success'
                return
            url.urlretrieve(image, os.path.normpath(os.path.normpath('H:/xkcd2/timeout')+'/'+str(self.num)+'.'+extension))
            self.status = 'Success'
            #print 'Retrieved http://imgs.xkcd.com/comics/'+str(self.num)+'/'
        else:
            #print 'Failed to fetch http://imgs.xkcd.com/comics/'+str(self.num)+'/'
            self.status = 'Failed to find match' 
 
def main():
    time1 = time.time()
    print time.ctime()
    file = open(os.path.normpath('H:/xkcd2/timeout/log.txt'), 'w')
    k = 0
    time3 = time1
    times = []
    while(k < 89):
        lst = []
        a = (10*k)+1
        b = (10*(k+1))+1
        print 'Running',str(a),'to',str((b-1))
        for i in range(a, b):
            obj = get(i)
            lst.append(obj)
            obj.start()
        for ob in lst:
            ob.join()
            print str(ob.num)+':',ob.status
            file.write('http://xkcd.com/'+str(ob.num)+': '+ob.status +'\n')
        time3 = time.time() - time3
        times.append(str('Finished ' + str(a) + ' to ' + str((b-1)) + ' in ' + str(time3) + ' seconds'))
        print 'Finished', str(a),'to',str((b-1)),'in',str(time3),'seconds'
        time3 = time.time()
        k += 1
    print time.ctime()
    for item in times:
        file.write(item + '\n')
    file.close()
    time2 = time.time()
    print time2 - time1

if __name__ == '__main__':
    main()